﻿#include "SerialController.h"

SerialController::SerialController(QObject *parent) : QObject(parent)
{
//    pPortUart = new QSerialPort();

//    pPortUart->setPortName("COM30");
//    pPortUart->setBaudRate(115200);
//    pPortUart->setDataBits(QSerialPort::DataBits::Data8);
//    pPortUart->setParity(QSerialPort::Parity::NoParity);
//    pPortUart->setStopBits(QSerialPort::StopBits::OneStop);
//    pPortUart->setFlowControl(QSerialPort::NoFlowControl);
//    if (pPortUart->open(QIODevice::OpenModeFlag::ReadWrite))
//    {
//        Logger::getInstance()->log(LogLevel::Info,QString("串口%1打开成功,波特率%2！"));
//    }
//    else
//    {
//        Logger::getInstance()->log(LogLevel::Warning,QString("串口%1打开失败,请检查串口COM号是否正确等信息！"));
    //    }
}

SerialController::~SerialController()
{
    if(pPortUart != nullptr)
    {
        pPortUart->close();
        delete pPortUart;
    }
    //mylog<<"serial delete";

}

bool SerialController::SerialInit(QString com,int band,int dataBits,int stopBits,int parity)
{
    pPortUart = new QSerialPort();

    pPortUart->setPortName(com);
    pPortUart->setBaudRate(band);
    QSerialPort::DataBits databit;
    switch (dataBits) {
    case 5:
        databit = QSerialPort::DataBits::Data5;
        break;
    case 6:
        databit = QSerialPort::DataBits::Data6;
        break;
    case 7:
        databit = QSerialPort::DataBits::Data7;
        break;
    case 8:
        databit = QSerialPort::DataBits::Data8;
        break;
    }
    pPortUart->setDataBits(databit);
    QSerialPort::Parity pair;
    switch (parity) {
    case 0:
        pair = QSerialPort::Parity::NoParity;
        break;
    case 2:
        pair = QSerialPort::Parity::EvenParity;
        break;
    case 3:
        pair = QSerialPort::Parity::OddParity;
        break;
    }
    pPortUart->setParity(pair);
    QSerialPort::StopBits stopbit;
    switch (stopBits) {
    case 1:
        stopbit = QSerialPort::StopBits::OneStop;
        break;
    case 2:
        stopbit = QSerialPort::StopBits::TwoStop;
        break;
    case 3:
        stopbit = QSerialPort::StopBits::OneAndHalfStop;
        break;
    }
    pPortUart->setStopBits(stopbit);
    pPortUart->setFlowControl(QSerialPort::NoFlowControl);
    return pPortUart->open(QIODevice::OpenModeFlag::ReadWrite);
}

QByteArray SerialController::sendAsync(QByteArray data)
{
    pPortUart->clear();
    pPortUart->write(data);
    //mylog<<"send:"<<data;
    QEventLoop eventLoop;
    QTimer timer;
    //connect(pPortUart, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
    connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
    timer.start(400);
    eventLoop.exec();

    timer.stop();
    QByteArray result = pPortUart->readAll();
    //mylog<<"recieve"<<result;
    return result;//pPortUart->readAll();
}


bool SerialController::serialController_init()
{
    return SerialInit("COM1",9600,8,1,0);
}


bool SerialController::CRC_16(QByteArray &arr)
{
    if(arr.size() == 0) return false;
    QByteArray array = arr.mid(0,arr.size()-2);
    uint CRC = 0xFFFF;

    int  i = 0;
    uint j = 0, tmp = 0;

    for (i = 0; i < array.count(); i++)
    {
        CRC ^= (uint8_t)array.data()[i];

        for (j = 0; j < 8; j++)
        {
            tmp = CRC & 0x0001;
            CRC = CRC >> 1;
            if (tmp != 0)
            {
                CRC = (CRC ^ 0xA001);
            }
         }
     }

    if(CRC == uint16_t(uint16_t(arr[arr.size()-1]<<8)+uint8_t(arr[arr.size()-2])))
        return true;
    else
    {
        arr[arr.size()-2] = CRC;
        arr[arr.size()-1] = CRC >> 8;
        return false;
    }
}
